package com.hanxiang.hankol.common;

import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baselibrary.activity.BaseActivity;
import com.baselibrary.base.BaseData;
import com.baselibrary.manager.SensorsDataManager;
import com.baselibrary.router.ActivityRouter;
import com.baselibrary.utils.ARouterUtils;
import com.baselibrary.utils.KeyBoardUtils;
import com.baselibrary.utils.SpUtils;
import com.baselibrary.utils.ToastUtils;
import com.hanxiang.hankol.R;
import com.hanxiang.hankol.databinding.ActivityJuBaoOneBinding;
import com.okhttp.SendRequest;
import com.okhttp.callbacks.GenericsCallback;
import com.okhttp.sample_okhttp.JsonGenericsSerializator;

import java.util.List;

import okhttp3.Call;

/**
 * 举报二
 */
@Route(path = ActivityRouter.ACTIVITY.REPORT)
public class JuBaoOneActivity extends BaseActivity implements View.OnClickListener {

    private ActivityJuBaoOneBinding mBinding;
    private JuBaoOneActivity activity;
    private int type;//举报类型
    private int typeId;//举报内容ID
    private int complaintType;//投诉类型
    private int complaintUserId;//被举报用户ID
    private TextView mLastView;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_ju_bao_one;
    }

    @Override
    protected void init() {
        mBinding = getDataBinding();
        activity = this;
        if (getIntent().getExtras() != null) {
            List<String> params = ARouterUtils.parseParams(getIntent().getExtras(), "type", "id", "complaintUserId");
            if (!TextUtils.isEmpty(params.get(0))) {
                if (!TextUtils.isEmpty(params.get(0))) {
                    type = Integer.parseInt(params.get(0));
                    typeId = Integer.parseInt(params.get(1));
                    complaintUserId = Integer.parseInt(params.get(2));
                }
            } else {
                type = getIntent().getExtras().getInt("type");
                typeId = getIntent().getExtras().getInt("id");
                complaintUserId = getIntent().getExtras().getInt("complaintUserId");
            }
        }

        initData();
        //获取页面开始时间
        SensorsDataManager.Companion.getInstance().onCreateTime();
    }

    private void initData() {
        mBinding.backImageView.setOnClickListener(this);
        mBinding.text1.setOnClickListener(this);
        mBinding.text2.setOnClickListener(this);
        mBinding.text3.setOnClickListener(this);
        mBinding.text4.setOnClickListener(this);
        mBinding.text5.setOnClickListener(this);
        mBinding.text6.setOnClickListener(this);
        mBinding.text7.setOnClickListener(this);
        mBinding.startTextView.setOnClickListener(this);
        mBinding.inputReason.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                showFlag();
                mBinding.textNumView.setText(String.valueOf(300 - s.length()));

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    @Override
    public void onClick(View v) {

//     *     public static final int complaintType_disuseqing = 1; //低俗色情
//     *     public static final int complaintType_lajiguanggao = 2; //垃圾广告
//     *     public static final int complaintType_rumagongji = 3; //辱骂攻击
//     *     public static final int complaintType_qitaweifaxinxi = 4; //其他违法信息
//     *     public static final int complaintType_chaoxi = 5; //抄袭
//     *     public static final int complaintType_baolutarenyinsi = 6; //暴露他人隐私
//     *     public static final int complaintType_other = 7; //其他

        switch (v.getId()) {
            case R.id.backImageView:
                finish();
                break;
            case R.id.text1:
                complaintType = 1;
                selectState();
                selectFun(mBinding.text1);
                break;
            case R.id.text2:
                complaintType = 2;
                selectState();
                selectFun(mBinding.text2);
                break;
            case R.id.text3:
                complaintType = 3;
                selectState();
                selectFun(mBinding.text3);
                break;
            case R.id.text4:
                complaintType = 4;
                selectState();
                selectFun(mBinding.text4);
                break;
            case R.id.text5:
                complaintType = 5;
                selectState();
                selectFun(mBinding.text5);
                break;
            case R.id.text6:
                complaintType = 6;
                selectState();
                selectFun(mBinding.text6);
                break;
            case R.id.text7:
                complaintType = 7;
                selectState();
                selectFun(mBinding.text7);
                break;
            case R.id.startTextView:
                //开始
                comFun();
                break;
        }
    }


    private void selectFun(TextView textView) {
        textView.setSelected(!textView.isSelected());
        if (textView.isSelected()) {
            textView.setBackground(getResources().getDrawable(R.drawable.line_7d1d29_90));
            textView.setTextColor(Color.parseColor("#F54D53"));
        } else {
            textView.setBackground(getResources().getDrawable(R.drawable.rect_f5f5f5_90));
            textView.setTextColor(Color.parseColor("#666666"));
        }
        showFlag();
        mLastView = textView;
    }

    private void showFlag() {
        if (mBinding.text1.isSelected() ||
                mBinding.text2.isSelected() ||
                mBinding.text3.isSelected() ||
                mBinding.text4.isSelected() ||
                mBinding.text5.isSelected() ||
                mBinding.text6.isSelected() ||
                mBinding.text7.isSelected()) {

            mBinding.startTextView.setBackground(getResources().getDrawable(R.drawable.rect_90_7d1d29));

        } else {
            String msg = mBinding.inputReason.getText().toString().trim();
            if (!TextUtils.isEmpty(msg)) {
                mBinding.startTextView.setBackground(getResources().getDrawable(R.drawable.rect_90_7d1d29));
            } else {
                mBinding.startTextView.setBackground(getResources().getDrawable(R.drawable.rect_90_7d1d29_81));
            }
        }
    }

    /**
     * 举报的fun
     */
    private void comFun() {

        String msg_type = "";
        if (mBinding.text1.isSelected()) {
            msg_type = msg_type + mBinding.text1.getText().toString().trim() + ",";
        }

        if (mBinding.text2.isSelected()) {
            msg_type = msg_type + mBinding.text1.getText().toString().trim() + ",";
        }

        if (mBinding.text3.isSelected()) {
            msg_type = msg_type + mBinding.text1.getText().toString().trim() + ",";
        }

        if (mBinding.text4.isSelected()) {
            msg_type = msg_type + mBinding.text1.getText().toString().trim() + ",";
        }

        if (mBinding.text5.isSelected()) {
            msg_type = msg_type + mBinding.text1.getText().toString().trim() + ",";
        }

        if (mBinding.text6.isSelected()) {
            msg_type = msg_type + mBinding.text1.getText().toString().trim() + ",";
        }

        if (mBinding.text7.isSelected()) {
            msg_type = msg_type + mBinding.text1.getText().toString().trim() + ",";
        }


        if (TextUtils.isEmpty(msg_type)) {
            ToastUtils.showShort(activity, "请选择举报类型");
            return;
        }


        String msg = mBinding.inputReason.getText().toString().trim();
        if (TextUtils.isEmpty(msg) && mBinding.text7.isSelected()) {
            ToastUtils.showShort(activity, "请描述理由");
            return;
        }


        SendRequest.complainAdd(SpUtils.getInstance().getLoadUserBean().getData().getToken(), complaintType, complaintUserId, type, typeId, msg, new GenericsCallback<BaseData>(new JsonGenericsSerializator()) {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(BaseData response, int id) {
                KeyBoardUtils.hideSoftKeyboard(JuBaoOneActivity.this);
                if (response.isSuccess()) {
                    ToastUtils.showShort(activity, "提交成功");
                    finish();
                } else {
                    ToastUtils.showShort(activity, response.getMsg());
                }
            }
        });

    }

    public void selectState() {
        if (mLastView != null) {
            mLastView.setSelected(false);
            mLastView.setBackground(getResources().getDrawable(R.drawable.rect_f5f5f5_90));
            mLastView.setTextColor(Color.parseColor("#666666"));
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        //获取页面结束时间
        SensorsDataManager.Companion.getInstance().onDestroyTime();
        if (SensorsDataManager.Companion.getInstance().getBrowseTime() != 0L) {
            SensorsDataManager.Companion.getInstance().brookPageView("好友举报页");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        KeyBoardUtils.hideSoftKeyboard(this);
        //获取页面结束时间
        SensorsDataManager.Companion.getInstance().onDestroyTime();
        if (SensorsDataManager.Companion.getInstance().getBrowseTime() != 0L) {
            SensorsDataManager.Companion.getInstance().brookPageView("好友举报页");
        }
    }
}
